//
//  ZYCCourseDetailParentBottomView.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/2.
//

#import "ZYCCourseDetailParentBottomView.h"
@interface ZYCCourseDetailParentBottomView ()

@property (nonatomic, strong) UIButton *statusButton;


@end
@implementation ZYCCourseDetailParentBottomView

- (void)setIsEnable:(BOOL)isEnable{
    _isEnable = isEnable;
    _statusButton.enabled = isEnable;
}
- (void)setTitle:(NSString *)title{
    _title = title;
    [_statusButton setTitle:title forState:UIControlStateNormal];

}

- (void)setStatus:(ZYCParentBottomStatus)status{
    _status = status;
    if (self.status == ZYCBottomStatusParentNoEnroll) {
        [_statusButton setTitle:@"报名" forState:UIControlStateNormal];
        [self addGreenGradientLayer];
    }else if(self.status == ZYCBottomStatusParentEnrolled){
        [_statusButton setTitle:@"取消报名" forState:UIControlStateNormal];
        [self addRedGradientLayer];
    }else if(self.status == ZYCBottomStatusParentEnrolledFull){
        [_statusButton setTitle:@"人员已满" forState:UIControlStateNormal];
        [self.statusButton setBackgroundColor:HEX_RGB(@"#CCCCCC")];
    }
}



- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = HEX_RGB(@"#F9F9F9");
        [self addSubview:self.statusButton];
        [self.statusButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(240.0f, 40.0f));
        }];
 
    }
    return self;
}


- (UIButton *)statusButton {
    if (!_statusButton) {
        _statusButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _statusButton.frame = CGRectMake(0,0, kScreenWidth - 2 * 68, 40);
        [_statusButton setTitleColor:HEX_RGB(kTitleWhiteColor) forState:UIControlStateNormal];
        [_statusButton addTarget:self action:@selector(clickStatusAction:) forControlEvents:UIControlEventTouchUpInside];
        _statusButton.layer.cornerRadius = 18;
        _statusButton.layer.masksToBounds = YES;

    }
    return _statusButton;
}

- (void)clickStatusAction:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(courseDetailParentBottomDidDetail:)]) {
        [self.delegate courseDetailParentBottomDidDetail:self.status];
    }
}

- (void)addRedGradientLayer{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#F5EBA7").CGColor,
                             (__bridge id)HEX_RGB(@"#F51111").CGColor];
    gradientLayer.locations = @[@(0.3),@(1.0f)];
    gradientLayer.frame = self.statusButton.bounds;
    gradientLayer.cornerRadius = 18;
    [self.statusButton.layer insertSublayer:gradientLayer atIndex:0];
}

- (void)addGreenGradientLayer{
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 0);
    gradientLayer.colors = @[(__bridge id)HEX_RGB(@"#F5EBA7").CGColor,
                             (__bridge id)HEX_RGB(@"#009043").CGColor];
    gradientLayer.locations = @[@(0.3),@(1.0f)];
    gradientLayer.frame = self.statusButton.bounds;
    gradientLayer.cornerRadius = 18;
    [self.statusButton.layer insertSublayer:gradientLayer atIndex:0];
}

@end
